package com.atguigu.gulimail.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimail.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000121604167";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCFeLEGN/RhYlBeua8xD9VG9lkEC8BTI2pZPGkD+3KolzsomE4nd2pmXKRALCKTOfa7TA5UXgAGHqxvyYYoiDiwNG6tQtqkUSobueNeP8WueLxHvBKcOlD6X+Fo9rDVSjzsy6nBk4KxArI7sGha3nkyHeJa6zsjA28pUH7/pM3kPDhqV6wnM4wrdsoXH0J+KeWVVS46df2bTgWG3mRQ5UIXvB5clpOzhc8vnHLl10N6WBVon7aJGs5utCvZt/TYSsEhOg6BuED+uPklhUHyJla3s6k4C6UqGAD1FNYf/F9XEit7etz1PYXvFiFznUo9ya54rbECAo0/OVWjYA6ntxWpAgMBAAECggEAL/BojNY+JVg9i7hfz1xje0hyMnzSCEp5ITIMegyvYxRmgC8sKwq+IaP2rsHrbTvtfdpUb0MP+SK9Rq14TUiYK9m5Quv3QfuRB0JeAvUe2Iqu/Yj0mj5r5XHYsW2hmApBFcAU6d1EnLVeNQgogzLTDwgizAsfQ3UxUQKDiuizttzOMVXi2r27D4426S9QEOfjPFLya8iTWSzuLnywdr1m4vN1H10Sdyh6siM2Seii1puGJvtEZjki299Y4p840+w90o6um3pO1uhCo9pbWeAegOmcOitg/f3zcsUDTcMVmwxNpGHQBmWniRmgIcvSd31qLecw+uYg35ILZQBjUH0MsQKBgQDnfH5tULP/GkUoN1054k7IT9+onVjebVZZ55o8Q1bP0t2HBVV35273D0LOz6HojvNH7+HELqDizdINDCuMuWu3VxPg6VcMkrK6aoBKPSA3B1m6YnVxBM61egAOB6ssEVgQucCIV9zbs3GDTb+Wg2xEil3jeg/lB2PB35mlm1MBzQKBgQCTmw1TlOa9l0EG7O0C2m83XtGkCR561Kd96FcwcbjMxYukdW6SAFM/rUQR8IaAIg+xawX4sESNmSM8KAr7iY9P7anK3XZVIiJ7oesrJWKbAbA9bG9l3V3mNeWMWNk9H8tCN3vKpR/ee1x6Q8vMSuF38jC1/AWKIPEuBfdTQU63TQKBgEAToPdneHur7OrF8INeVopyOan17CydvWJjjrkRts0ORI3kdxUatgejbM3MgbfbIhKOn0Lr2VMOwA8miEOK6/szGmiOjq8HSHv7kQl7xBgXa8oBCRtAxmtLmwpHKyWmo2b11Aqpr5QNJuW+tsLWHDkYyAZ4uj+efQmkeGZ0CVUdAoGAQOs4+UHMGiakGv+pXk/kCl5Rpeu81BkjcWT2s//QX3+i4zoi32P1OVRjU+CNx10zubxwa+havkxUCukxq1/dADJvMyIkIV1Z/TZB0EApFjOv59QBSrQJ0G5z/z40jYWmSAFPGNl3Isru6XdX8wocED0c321wokfKy4skNCHNFSECgYAp81JqQMnX1Od2N/R9LsxhpnTeuYjeuqacmxwztzrSSp3G8zanipgFjxRLefCM4VX86oaLzeAAR6pqPqB2U3ItO2q2Kw3eK/51yCuv2bxeJ0Hon8QOM6yb2O9H6kqLwyTrO/KO87kJmRHj1z6btMVTq9IvRJo4NGNL9td+idjscw==";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAg44/AY62foeY5xOEUp9yyegVckZz3xT8PTt0oeFZqzZp/P5bjb7B/PbnEZfMBX43jxLQqO5f8tfBLBFMm/we5w6N4/PGAMpaMu/2UyTNwbhbjlxiFhozlUiTrV2NPg1b7pbL+tyVTFX4vd/nMIZXyUds/nCO/DPSYTeKFbjbjehq4Ks7Hv6EAOuPTUPZX70IehiteWOhYAXX04bbAzitQTFlxR501qUqiVEq/Xpu6Vsm4OjEEWogyaDRiXYLm/RWUrn26Zvl1fPct5DmuACnLhUTRb6OY2ZIzwI3hbh87dAAq5TBjjuFVuUxxMYnfOmB4dfJO7/sXEktFOlDp+Nt+QIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://asdasdas.free.aeert.com/payed/notify";
    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimail.com/memberOrder.html";
    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
